<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<html>
<head>
    <%@include file="/include/header.html" %>
</head>

<body class="gray-bg">

<div class="panel panel-default center">
    <form class="form-horizontal m-t" id="frmMenu">
        <input id="parentId" name="parentId" type="hidden" value="${parentId}"/>
        <input id="tenantMenuId" name="tenantMenuId" type="hidden" value="${menu.tenantMenuId}" />

        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-right">上级菜单：</label>
            <div class="col-sm-8"><input id="" name="" class="form-control" type="text" value="${menu.menuName}" readonly>
            </div>
        </div>
        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-right">授权文件(JSP/JS)：</label>
            <div class="col-sm-8">
                <textarea name="fileNames" id="fileNames" rows="2" style="width:100%;height:300%" required></textarea>
                <a class="btn btn-primary btn-sm " href="#" title="分析" onclick="scanPermission()">分析</a>
                <li><i class="fa fa-bell-o"></i>支持多个JSP/JS文件,使用半角逗号分隔</li>
                <li></i>如：src/main/webapp/templates/platform/menu/menu.jsp</li>
            </div>
        </div>

        <div class="form-group row">
            <label class="col-sm-3 col-form-label text-right">分析结果：</label>
            <div class="col-sm-8">
                <textarea name="result" id="result" rows="5" style="width:100%;height:300%"></textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-4 col-sm-offset-8 text-right">
                <button type="submit" class="btn btn-success">提交</button>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var prefix = jsctx + "/tenant/menu";

    (function () {
        var icon = "<i class='fa fa-times-circle'></i> ";
        $("#frmMenu").validate({
            rules: {
                fileNames: {
                    required: true
                },
                result: {
                    required: true
                }
            },
            messages: {
                fileNames: {
                    required: icon + "请输入授权文件"
                },
                result: {
                    required: icon + "请输入分析结果"
                }
            },
            submitHandler: function (form) {
                save();
            }
        })
    })();


    function save() {
        $.ajax({
            cache: true,
            type: "POST",
            url: prefix + "/savePermission",
            data: $('#frmMenu').serialize(),
            async: false,
            error: function (request) {
                parent.layer.alert("Connection error");
            },
            success: function (data) {
                if (data.status == 200) {
                    parent.layer.msg("保存成功");
                    // menu.goBack();

                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                } else {
                    parent.layer.alert(data.message)
                }
            }
        });
    }

    function scanPermission() {
        var fileNames = $('#fileNames').val();
        if (!fileNames) {
            parent.layer.alert("授权文件名称不能为空")
            return;
        }

        $.ajax({
            type: "POST",
            url: "${ctx}/tenant/menu/scanPermission",
            data: $('#fileNames').serialize(),
            error: function (request) {
                alert("网络异常");
            },
            success: function (data) {
                if (data.status == 200) {
                    $('#result').val(data.data);
                } else {
                    // alert(data.message);
                    parent.layer.alert(data.message)
                }
            }
        });
    };
</script>

</body>
</html>
